<?php
//Фабрика по работе с картами
class Map{
	protected static $_instance;
	protected static $_maps;
	private function __construct(){}
	private function __clone(){}
	
	public static function inst(){
	  if( null===self::$_instance ){
	    self::$_instance=new self();
		}
		return self::$_instance;
	}
	
	protected $errors=array('err'=>array(),'wrn'=>array());
	protected function error($msg){ $this->errors['err'][]=$msg; return false;}
	protected function warning($msg){ $this->errors['wrn'][]=$msg; return false;}
	public function getError(){ return $this->errors['err'][ count($this->errors['err'])-1 ]; }
	public function getErrors(){ return $this->errors['err']; }
	public function getWarning(){ return $this->errors['wrn'][ count($this->errors['wrn'])-1 ]; }
	public function getWarnings(){ return $this->errors['wrn']; }


	public function getMap($map){
		if( !empty($this->_maps[$map]) ) return $this->maps[$map];

	  $mapName= mb_strtoupper( mb_substr($map, 0, 1, 'utf8') , 'utf8').  mb_substr($map, 1, 50, 'utf8');
	  $mapFileName=$mapName.'.php';
	  if( file_exists($_SERVER['DOCUMENT_ROOT'].'/lib/'.$mapFileName) )
	    include_once $_SERVER['DOCUMENT_ROOT'].'/lib/'.$mapFileName;
	  if( !class_exists($mapName) )throw_error('class '.$mapName.' not found',500);
	  return $mapName::inst();
	}
	
	public function geocode($params, $m='google'){
	  $map=$this->getMap($m);
	  $rs=$map->geocode($params);
	  if(!$rs) return $this->error( $map->getError() );
	  return $rs;
	}
	public function find($params, $m='google'){
	  $map=$this->getMap($m);
	  $rs=$map->find($params);
	  if(!$rs) return $this->error( $map->getError() );
	  return $rs;
	}
}
?>
